package com.door.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.door.erp.dwz.DwzResponse;
import com.door.erp.enums.BaseInfoStatus;
import com.door.erp.model.ExpenditureType;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.ExpenditureTypeService;

/**
 *类说明
 *
 * @author Xuelin Yang E-mail:yangxuelin@foxmail.com
 * @version 创建时间：2013-6-3 上午9:45:46
 */
@Controller
public class ExpenditureTypeController {
	
	@Resource(name="expenditureTypeService", type=ExpenditureTypeService.class)
	private ExpenditureTypeService expenditureTypeService;
	
	@RequestMapping("/expenditureType/expenditureTypes.do")
	public ModelAndView expenditureTypes(Pager pager) {
		if(pager == null) {
			pager = new Pager();
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", BaseInfoStatus.NORMAL);
		int totalCount = expenditureTypeService.getCount(params);
		pager.setTotalCount(totalCount);
		List<ExpenditureType> expenditureTypes = expenditureTypeService.getAll(params, pager);

		ModelAndView mav = new ModelAndView();
		mav.setViewName("expendituretype/expendituretypes");
		mav.addObject("pager", pager);
		mav.addObject("expenditureTypes", expenditureTypes);
		return mav;
	}
	
	@RequestMapping("/expenditureType/addPage.do")
	public ModelAndView getAddPage() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("expendituretype/add");
		return mav;
	}
	
	@RequestMapping("/expenditureType/add.do")
	public ModelAndView add(ExpenditureType expenditureType) {
		expenditureType.setStatus(BaseInfoStatus.NORMAL);
		expenditureTypeService.add(expenditureType);
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("expenditureType");
		resonse.setMessage("添加开销类型成功");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/expenditureType/getExpenditureType.do")
	public ModelAndView getExpenditureType(@RequestParam(value="id", required=true) Long id) {
		ExpenditureType expenditureType = expenditureTypeService.getObject(id);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("expendituretype/modify");
		mav.addObject("expenditureType", expenditureType);
		return mav;
	}
	
	@RequestMapping("/expenditureType/modify.do")
	public ModelAndView modify(ExpenditureType expenditureType) {
		expenditureTypeService.modify(expenditureType);
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("expenditureType");
		resonse.setMessage("修改开销类型成功");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/expenditureType/delete.do")
	public ModelAndView delete(long id) {
		ExpenditureType expenditureType = expenditureTypeService.getObject(id);
		expenditureType.setStatus(BaseInfoStatus.DELETED);
		expenditureTypeService.modify(expenditureType);
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("expenditureType");
		resonse.setMessage("删除开销类型成功");
		resonse.setCallbackType("");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/expenditureType/lookuplist.do")
	public ModelAndView lookuplist(Pager pager) {
		if(pager == null) {
			pager = new Pager();
		}
		ModelAndView mv = new ModelAndView("expendituretype/expendituretype_lookup");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", BaseInfoStatus.NORMAL);
		int totalCount = expenditureTypeService.getCount(params);
		pager.setTotalCount(totalCount);
		List<ExpenditureType> expenditureTypes = expenditureTypeService.getAll(params, pager);
		mv.addObject("expenditureTypes", expenditureTypes);
		return mv;
	}

}
